﻿/*
 * @Description:楼盘详情页面js
 * @Author:yangpl
 * @Date:2014-08-18
 */

var pageShowEstateForView = {};

function setPageStartTime() {
    window.pageStartTime = new Date();
}
function setPageEndTime() {
    $("#timeCount").html("查询耗时" + timehelper.TimeDiffirence(window.pageStartTime, new Date()).msDiff + "毫秒");
}

pageShowEstateForView.init = function () {
    var option = {
        titleID: "titleid",
        contentID: "contentdiv",
        currentClassName: "current",
        titleFun: pageNewEstateDetail.getTitles,
        urlFun: pageNewEstateDetail.getTabUrl,
        dataFun: pageNewEstateDetail.getData,
        contentFun: null
    };
    tabHelper(option);
    //栋座搜索智能提示
    pageShowEstateForView.searchBuildingAutoMatch($("#hidEstateKeyId").val());
};

pageShowEstateForView.getTitles = function () {
    //return ["基本信息", "周边设施", "小区图片"];
    return ["基本信息",  "小区图片"];
};

pageShowEstateForView.getTabUrl = function (tabname) {
    var urlNum = window.location.href.split('=')[1];
    if (tabname == "基本信息") {

        return "/estate/get-estate-base-info?KeyId=" + urlNum;
    }
    //if (tabname == "周边设施") {
    //    return "/estate/find-estate-around-for-view-new?KeyId=" + urlNum;
    //}
    if (tabname == "小区图片") {
        return "/estate/find-estate-photo-for-view?EstateKeyId=" + urlNum + "&PhotoType=1&EstateName=" + escape($("#spanEstateName").html());
    }
};

pageShowEstateForView.getData = function (tabname) {
    if (tabname != "栋座") {
        return null;
    } else {
        return [{ name: "key", value: tabname }];
    }
};

//栋座搜索智能提示
pageShowEstateForView.searchBuildingAutoMatch = function (estateKeyId) {
    //栋座搜索智能提示Url
    pageShowEstateForView.getBuildingUrl = function () {
        return "/Estate/auto-match-building-name?EstateKeyId=" + estateKeyId + "&BuildingName=" + escape($("#txtBuildingName").val());
    };
    //楼盘自动完成及赋值隐藏域
    $("#txtBuildingName").autocomplete(pageShowEstateForView.getBuildingUrl, {
        width: 100,
        max: 1000,
        Default: 1,
        selectFirst: false,
        cacheLength: 0,
        formatItem: function (row) {
            return row.ItemText;
        },
        formatResult: function (row) {
            return row.ItemText;
        }
    });
};

//搜索栋座
pageShowEstateForView.searchBuilding = function (estateKeyId) {
    $("#btnSearchBuilding").unbind("click");
    $("#btnSearchBuilding").bind("click", function () {
        var reqData = {
            "KeyId": estateKeyId,
            "BuildingName": $("#txtBuildingName").val()
        };
        $.AjaxAgency("/estate/find-estate-for-view", reqData, function (data) {
            if (data.indexOf("#error:") > -1) {
                $.OKDialogAgency('<div class="tc">加载失败，请重新加载！</div>');
            } else {
                $("#ajaxPages .pages").remove();
                $(data).replaceAll("#gdList");
            }
        });
    });
};

//插入房间列表
pageShowEstateForView.insertRoomList = function () {
    $("#gdList .btnRoomList").unbind("click");
    $("#gdList .btnRoomList").bind("click", function () {
        var currentElement = $(this);
        var keyId = currentElement.attr("keyid");
        var isOpenCurrent = $(this).attr("room-open");
        if (isOpenCurrent == "true") {
            $(this).attr("room-open", "false");
            currentElement.parent().parent().next().remove();
            return false;
        }
        //找到插入的房间表格进行删除，房间表格不唯一会导致js混乱
        $("#gdList .btnRoomList").each(function () {
            if ($(this).attr("room-open") == "true") {
                $(this).attr("room-open", "false");
                $(this).parent().parent().next().remove();
                
            }

        });

        var reqData = {
            "ColumnCount": 17,
            "BuildingKeyId": keyId,
            "BuildingTypeCode": currentElement.attr("building-type-code"),
            "RoomNoRuleItemValue": currentElement.attr("roomNoRuleItemValue"),
            "FirstFloor": currentElement.attr("first-floor")
        };
        $.AjaxAgency("/estate/show-house-paged-by-condition", reqData, function (data) {
            if (data.indexOf("#error:") > -1) {
                $.OKDialogAgency('<div class="tc">加载失败，请重新加载！</div>');
            } else {
                if (currentElement.attr("room-open") != "true") {
                    currentElement.parent().parent().after(data);
                    currentElement.attr("room-open", "true");
                    $("#OriginBuildingKeyId").val(keyId);
                }
            }

        });

    });
};

//房间页面初始化
pageShowEstateForView.RoomPageInit = function () {
    var $btnRoomList = $("tr.follow-status").prev().find(".btnRoomList").eq(0);
    //房间搜索智能提示
    pageShowEstateForView.searchHouseAutoMatch($btnRoomList.attr("keyid"));

    //房间搜索
    pageShowEstateForView.searchHouse();

};

//房间搜索智能提示
pageShowEstateForView.searchHouseAutoMatch = function (buildingKeyId) {
    //房间搜索智能提示Url
    pageShowEstateForView.getHouseUrl = function () {
        return "/Estate/auto-match-house-no?BuildingKeyId=" + buildingKeyId + "&HouseNo=" + escape($("#txtHouseNo").val());
    };
    //楼盘自动完成及赋值隐藏域
    $("#txtHouseNo").autocomplete(pageShowEstateForView.getHouseUrl, {
        width: 260,
        max: 1000,
        Default: 1,
        selectFirst: false,
        cacheLength: 0,
        formatItem: function (row) {
            return row.ItemText;
        },
        formatResult: function (row) {
            return row.ItemText;
        }
    });
};

//搜索房间
pageShowEstateForView.searchHouse = function () {
    $("#btnSearchHouse").unbind("click");
    $("#btnSearchHouse").bind("click", function () {
        var $btnRoomList = $("tr.follow-status").prev().find(".btnRoomList").eq(0);
        var reqData = {
            "BuildingKeyId": $btnRoomList.attr("keyid"),
            "BuildingTypeCode": $btnRoomList.attr("building-type-code"),
            "RoomNoRuleItemValue": $btnRoomList.attr("roomNoRuleItemValue"),
            "HouseNo": $("#txtHouseNo").val(),
            "IsSearch": true
        };
        $.AjaxAgency("/estate/show-house-paged-by-condition", reqData, function (data) {
            if (data.indexOf("#error:") > -1) {
                $.OKDialogAgency('<div class="tc">加载失败，请重新加载！</div>');
            } else {
                $(data).replaceAll("#gdRoomList");                
               
            }
        });
    });
};

//搜索房间
pageShowEstateForView.PrevPage = function (page) {
    $("#btnPrev").unbind("click");
    $("#btnPrev").bind("click", function () {
        var pageIndex = page - 1;
        var $btnRoomList = $("tr.follow-status").prev().find(".btnRoomList").eq(0);
        var reqData = {
            "BuildingKeyId": $btnRoomList.attr("keyid"),
            "BuildingTypeCode": $btnRoomList.attr("building-type-code"),
            "RoomNoRuleItemValue": $btnRoomList.attr("roomNoRuleItemValue"),
            "HouseNo": $("#txtHouseNo").val(),
            "IsSearch": true
        };
        $.AjaxAgency("/estate/show-house-paged-by-condition?pageindex=" + pageIndex, reqData, function (data) {
            if (data.indexOf("#error:") > -1) {
                $.OKDialogAgency('<div class="tc">加载失败，请重新加载！</div>');
            } else {
                $(data).replaceAll("#gdRoomList");

            }
        });
    });
};

//搜索房间
pageShowEstateForView.NextPage = function (page) {
    $("#btnNext").unbind("click");
    $("#btnNext").bind("click", function () {
        var pageIndex = page + 1;
        var $btnRoomList = $("tr.follow-status").prev().find(".btnRoomList").eq(0);
        var reqData = {
            "BuildingKeyId": $btnRoomList.attr("keyid"),
            "BuildingTypeCode": $btnRoomList.attr("building-type-code"),
            "RoomNoRuleItemValue": $btnRoomList.attr("roomNoRuleItemValue"),
            "HouseNo": $("#txtHouseNo").val(),
            "IsSearch": true
        };
        $.AjaxAgency("/estate/show-house-paged-by-condition?pageindex=" + pageIndex, reqData, function (data) {
            if (data.indexOf("#error:") > -1) {
                $.OKDialogAgency('<div class="tc">加载失败，请重新加载！</div>');
            } else {
                $(data).replaceAll("#gdRoomList");

            }
        });
    });
};